<script setup>
import { onMounted, ref, onUnmounted } from 'vue';
import * as echarts from 'echarts';
import { empJobCountApi, empSexCountApi, stuCountApi, stuEducationCountApi } from '@/api/report';

const chartRefJob = ref(null);
const chartRefSex = ref(null);

// 员工职位统计表
let empJobChart = null;
// 员工性别统计表
let empSexChart = null;

const queryEmpJobCount = async() => {
  try {
    const res = await empJobCountApi();
    console.log(res);
    
    empJobChart.setOption({
      title: {
        text: '员工职位统计',
        left: 'center',
        textStyle: {
          fontSize: 18,
          fontWeight: 'bold'
        }
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        type: 'category',
        data: res.data.jobList,
        axisTick: {
          alignWithLabel: true
        }
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        name: '人数',
        type: 'bar',
        barWidth: '60%',
        data: res.data.dataList,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#83bff6' },
            { offset: 0.5, color: '#188df0' },
            { offset: 1, color: '#188df0' }
          ])
        },
        emphasis: {
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#2378f7' },
              { offset: 0.7, color: '#2378f7' },
              { offset: 1, color: '#83bff6' }
            ])
          }
        }
      }]
    });
  } catch (error) {
    console.error('获取员工职位统计数据失败:', error);
  }
}

const queryEmpSexCount = async() => {
  try {
    const res = await empSexCountApi();
    console.log(res);
    
    empSexChart.setOption({
      title: {
        text: '员工性别统计',
        left: 'center',
        textStyle: {
          fontSize: 18,
          fontWeight: 'bold'
        }
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        top: 'bottom',
        left: 'center'
      },
      series: [
        {
          name: '性别分布',
          type: 'pie',
          radius: ['40%', '70%'],
          center: ['50%', '55%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 20,
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: res.data
        }
      ]
    });
  } catch (error) {
    console.error('获取员工性别统计数据失败:', error);
  }
}

onMounted(() => { 
  // 员工职位统计
  if (chartRefJob.value) {
    empJobChart = echarts.init(chartRefJob.value);
    queryEmpJobCount();
  } 
  
  // 员工性别统计
  if (chartRefSex.value) {
    empSexChart = echarts.init(chartRefSex.value);
    queryEmpSexCount();
  }
});

onUnmounted(() => { 
  if (empJobChart) {
    empJobChart.dispose();
  }
  if (empSexChart) {
    empSexChart.dispose();
  }
});
</script>

<template>
  <!-- 员工职位/性别统计 用echarts -->
  <div class="report-container">
    <div class="header-section">
      <h2 class="page-title">员工统计报表</h2>
    </div>
    
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>员工职位统计</span>
            </div>
          </template>
          <div ref="chartRefJob" class="chart-container"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>员工性别统计</span>
            </div>
          </template>
          <div ref="chartRefSex" class="chart-container"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
.report-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100%;
}

.header-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px 20px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.page-title {
  color: #303133;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.chart-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  border: none;
}

.chart-card :deep(.el-card__header) {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
  background-color: #fafafa;
}

.card-header {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.chart-container {
  width: 100%;
  height: 400px;
}

@media (max-width: 768px) {
  .report-container {
    padding: 10px;
  }
  
  .header-section {
    padding: 10px 15px;
  }
  
  .page-title {
    font-size: 20px;
  }
  
  .el-row {
    flex-direction: column;
  }
  
  .el-col {
    width: 100%;
    margin-bottom: 15px;
  }
  
  .chart-container {
    height: 300px;
  }
}
</style>